<template>
  <px-dialog v-model="visible" class="equip-dialog-wrap" title="SOP选择" width="1200px" @open="openDialog" @close="closeDialog">
    <div class="equip-dialog-content">
      <px-form ref="formRef" class="custom-form-ui equip-form" :model="queryParams" label-position="right" label-width="100px">
        <px-row :gutter="16">
          <px-col :span="8">
            <px-form-item label="SOP文档" prop="name">
              <px-input v-model="queryParams.name" placeholder="请输入文档名称/关键词" maxlength="50" />
            </px-form-item>
          </px-col>
          <px-col :span="8">
            <px-form-item label="文件夹" prop="id">
              <px-select v-model="queryParams.id" placeholder="请选择文件夹">
                <px-option v-for="item in DMSFileNameList" :key="item.id" :label="item.name" :value="item.value" />
              </px-select>
            </px-form-item>
          </px-col>
          <px-col :span="8">
            <div class="equip-dialog-btn">
              <px-button type="primary" @click="queryTable">查询</px-button>
              <px-button @click="resetTable">重置</px-button>
            </div>
          </px-col>
        </px-row>
      </px-form>
      <px-table ref="tableRef" :data="tableData" class="equip-table" size="small" max-height="400" border @selection-change="selectChange">
        <px-table-column type="selection" width="60" align="center" />
        <px-table-column
          v-for="(item, index) in tableColumns"
          :key="index"
          :label="item.label"
          :min-width="item.minWidth"
          :align="item.align"
          :show-overflow-tooltip="item.showOverflowTooltip"
          :fixed="item.fixed"
        >
          <template #default="scope">
            <span v-if="item.type === 'size'">
              {{ scope.row.fileSize !== null ? byteToMb(scope.row.fileSize) : "" }}
            </span>
            <span v-else>
              {{ scope.row[item.prop] === 0 ? scope.row[item.prop] : scope.row[item.prop] ? scope.row[item.prop] : "-" }}
            </span>
          </template>
        </px-table-column>
        <px-table-column fixed="right" label="操作" min-width="160" align="center">
          <template #default="scope">
            <px-tooltip v-if="scope.row.workOrderStatus !== 0" content="查看" placement="top">
              <px-button link type="primary" icon="Tickets" @click="handleView(scope.row)" />
            </px-tooltip>
          </template>
        </px-table-column>
      </px-table>
      <pagination
        v-show="total > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        class="equip-pagination"
        :total="total"
        @pagination="queryTable"
      />
      <div style="margin-top: 16px; text-align: right">
        <px-button @click="closeDialog">取消</px-button>
        <px-button type="primary" @click="handleSubmit">确认</px-button>
      </div>
    </div>
    <!-- 文件详情 -->
    <FileDetail v-model:visible="fileDetailVisible" :fileDetailData="fileDetailData" />
  </px-dialog>
</template>
<script setup lang="ts">
import { queryDictData } from "@/api/common";
import { fileDetailAPI, searchDocAPI } from "@/api/knowledge/documentManage";
import FileDetail from "@/views/all/knowledge/documentManage/component/fileDetail.vue";
import { computed, reactive, toRefs } from "vue";

// 调用父级-关闭弹窗方法
const emits = defineEmits(["closeDialog", "addFileDialog"]);
// 父级传过来的值
const props = defineProps(["visible"]);
const visible = computed(() => props.visible);
// 页面本身的值
const state = reactive<any>({
  tableRef: null,
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    type: "manage"
  },
  tableColumns: [
    { prop: "name", label: "文件名称", minWidth: "180", showOverflowTooltip: true },
    { prop: "fileSize", label: "文档大小(M)", minWidth: "140", type: "size", showOverflowTooltip: true },
    { prop: "updateTime", label: "修改时间", minWidth: "140", showOverflowTooltip: true },
    { prop: "updateByName", label: "修改人", minWidth: "140", showOverflowTooltip: true }
  ],
  tableData: [],
  total: 0,
  selectRows: [],
  DMSFileNameList: [],
  fileDetailVisible: false,
  fileDetailData: {},
  formRef: null
});
const { tableRef, formRef, queryParams, tableColumns, tableData, total, selectRows, DMSFileNameList, fileDetailVisible, fileDetailData } =
  toRefs(state);

// 查询工具列表
const queryTable = async () => {
  const params = queryParams.value;
  const data: any = await searchDocAPI(params);
  tableData.value = data?.data || [];
  total.value = data?.total;
};
// 重置筛选条件和工具列表
const resetTable = async () => {
  queryParams.value = {
    type: "manage",
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize
  };
  queryTable();
};
// 表格多选框选中
const selectChange = () => {
  selectRows.value = tableRef.value.getSelectionRows();
};

const byteToMb = bytes => {
  return (bytes / 1024 / 1024).toFixed(2);
};

// 确定选择备件
const handleSubmit = () => {
  emits("closeDialog");
  emits("addFileDialog", selectRows.value);
};

const getDMSFileNameList = async () => {
  DMSFileNameList.value = await queryDictData({ typeCode: "MAINTENANCE_SOP_ID" });
};

// 文件详情
const handleView = async row => {
  state.fileDetailData = await fileDetailAPI({ id: row.id });
  state.fileDetailVisible = true;
};

// 打开弹窗
const openDialog = async () => {
  getDMSFileNameList();
  queryTable();
};
// 关闭弹窗
const closeDialog = () => {
  formRef.value.resetFields();
  tableData.value = [];
  emits("closeDialog");
};
</script>
<style lang="scss" scoped>
.equip-dialog-wrap {
  .equip-dialog-content {
    :deep(.px-form) {
      width: 100%;

      .px-form-item {
        width: 100%;
        margin-right: 0;

        .px-input {
          width: 100%;
        }

        .px-select {
          width: 100%;
        }

        .px-select-v2 {
          width: 100%;
        }
      }
    }

    .equip-dialog-btn {
      :deep(.px-button + .px-button) {
        margin-left: 8px;
      }
    }

    .equip-table {
      --px-table-border-color: #dfdfdf;

      :deep(.px-table__header) {
        thead {
          tr {
            background-color: #e7eefb !important;

            th {
              font-size: 13px;
              font-weight: bold;
              color: #000;
              background-color: #e7eefb !important;
            }
          }
        }
      }
    }

    :deep(.px-pagination) {
      right: 32px;
    }
  }
}
</style>
